package com.plata.base.aclr.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class SPHelper {
    public static final String KEY_UDID = "udid";
    public static final String KEY_LOG_LEVEL = "log_level";
    public static final String KEY_LIB_LOCAL_VERSION = "lib_local_version";
    public static final String KEY_APP_IS_DELETE_LOCAL = "is_delete_local";
    public static final String KEY_P2P_UPDATE = "is_p2p_update";

    private static SPHelper sSingleton;
    protected final SharedPreferences mSharedPreferences;
    protected final Editor mEditor;

    protected SPHelper(Context context) {
        this.mSharedPreferences = context.getApplicationContext().getSharedPreferences(getSpName(), Context.MODE_PRIVATE);
        this.mEditor = this.mSharedPreferences.edit();
    }

    protected String getSpName() {
        return "p2p_config";
    }

    public static SPHelper getInstance(Context context) {
        if (sSingleton == null) {
            synchronized (SPHelper.class) {
                if (sSingleton == null) {
                    if (context == null) {
                        throw new IllegalArgumentException();
                    }
                    sSingleton = new SPHelper(context);
                }
            }
        }
        return sSingleton;
    }

    public boolean contains(String key) {
        return this.mSharedPreferences.contains(key);
    }

    public boolean commit() {
        return this.mEditor.commit();
    }

    public void apply() {
        this.mEditor.apply();
    }

    public SPHelper remove(String key) {
        this.mEditor.remove(key);
        return this;
    }

    public boolean removeAndCommit(String key) {
        this.mEditor.remove(key);
        return this.mEditor.commit();
    }

    public SPHelper clear() {
        this.mEditor.clear();
        return this;
    }

    public SPHelper putString(String key, String value) {
        this.mEditor.putString(key, value);
        return this;
    }

    public SPHelper putBoolean(String key, boolean value) {
        this.mEditor.putBoolean(key, value);
        return this;
    }

    /**
     * Set a String value in the preferences
     *
     * @param key   key
     * @param value value of String
     * @return is saved success
     */
    public boolean putStringAndCommit(String key, String value) {
        this.mEditor.putString(key, value);
        return this.mEditor.commit();
    }

    /**
     * Set a Boolean value in the preferences
     *
     * @param key   key
     * @param value value of boolean
     * @return is saved success
     */
    public boolean putBooleanAndCommit(String key, boolean value) {
        this.mEditor.putBoolean(key, value);
        return this.mEditor.commit();
    }

    public String getString(String key) {
        return this.mSharedPreferences.getString(key, "");
    }

    public String getString(String key, String defaultValue) {
        return this.mSharedPreferences.getString(key, defaultValue);
    }

    public boolean getBoolean(String key) {
        return this.mSharedPreferences.getBoolean(key, false);
    }

    public boolean getBoolean(String key, boolean defaultValue) {
        return this.mSharedPreferences.getBoolean(key, defaultValue);
    }

    public int getInt(String key) {
        return this.mSharedPreferences.getInt(key, 0);
    }

    public int getInt(String key, int defaultValue) {
        return this.mSharedPreferences.getInt(key, defaultValue);
    }

    public SPHelper putInt(String key, int value) {
        this.mEditor.putInt(key, value);
        return this;
    }

    public long getLong(String key, int defaultValue) {
        return this.mSharedPreferences.getLong(key, defaultValue);
    }

    public SPHelper putLong(String key, long value) {
        this.mEditor.putLong(key, value);
        return this;
    }

    public SPHelper putFloat(String key, float f) {
        mEditor.putFloat(key, f);
        return this;
    }

    public float getFloat(String key, float f) {
        return mSharedPreferences.getFloat(key, f);
    }
}
